package dfsketches.gr3d;

public class MovableObject {
	private Instance inst;
	private Vector3D position;
	private float rotationAngle;
	private Activatable activCode;
	
	public MovableObject(Instance i) {
		this(i, null);
	}
	
	public MovableObject(Instance i, Activatable actor) {
		inst = i;
		position = new Vector3D(0f, 0f, 0f);
		rotationAngle = 0f;
		activCode = actor;
	}
	
	public void moveTo(Vector3D pos) {
		position = pos;
		inst.setMatrix(Matrix3D.getTranslationMatrix(position.x, position.y, position.z));
	}
	
	public void move(Vector3D transl) {
		position.add(transl);
		inst.apply(Matrix3D.getTranslationMatrix(transl.x, transl.y, transl.z));
	}
	
	public void scale(float scale) {
		inst.apply(Matrix3D.getScaleMatrix(scale, scale, scale));
		position = Vector3D.getScaled(position, scale);
	}
	
	public void rotateY(float angle) {
		rotationAngle += angle;
		inst.apply(Matrix3D.getPolarRotationYMatrix(angle, position.x, position.z));
	}
	
	public void playAnimation(String name) {
		inst.playAnimation(name, 0f, 0.001f);
	}
	
	public void playAnimation(String name, float start, float speed) {
		inst.playAnimation(name, start, speed);
	}
	
	public void changeAnimationSpeed(float newSpeed) {
		inst.changeAnimationSpeed(newSpeed);
	}
	
	public void stopAnimation() {
		inst.stopAnimation();
	}
	
	public Vector3D getPosition() {
		return new Vector3D(position);
	}
	
	public long getNumber() {
		return inst.getInstanceNumber();
	}
	
	public void addSubobject(MovableObject mob) {
		inst.addSubinstance(mob.inst);
	}
	
	public void activate() {
		if (activCode != null)
			activCode.activate();
	}

	public void setActivator(Activatable act) {
		activCode = act;
	}
	
}